package exporter

import (
	"emc_vnx_exporter/utils"
	"github.com/prometheus/client_golang/prometheus"
	"io/ioutil"
	"log"
	"os/exec"
	"strings"
	"time"
)

type Power struct {
	Name  string
	State string
}

var (
	power = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "vnx_get_all_power",
			Help: "Get All Power Info",
		},
		[]string{"Name", "State"},
	)
)

func PowerGauge() *prometheus.GaugeVec {
	return power
}

func GetAllPowerMetrics(spIp string, user string, password string) {
	for {
		cmd := exec.Command("bash", "-c", "/opt/Navisphere/bin/naviseccli -h "+spIp+" -user "+user+" -password "+password+" -Scope 0 getcrus | grep 'Power*'")
		stdout, _ := cmd.StdoutPipe()
		defer stdout.Close()
		if err := cmd.Start(); err != nil {
			log.Fatalf("cmd.Start: %v", err)
		}
		result, _ := ioutil.ReadAll(stdout) // 读取输出结果
		resData := string(result)

		powerList := strings.Split(resData, "\n")
		for _, powerString := range powerList {
			if strings.Contains(powerString, "State:") {
				powerItems := strings.Split(powerString, "State: ")
				data := new(Power)
				data.Name = powerItems[0]
				data.State = powerItems[1]
				power.WithLabelValues(data.Name, data.State).Set(utils.CrusStateToFloat(data.State))
			}
		}

		time.Sleep(60 * time.Second)
	}
}
